<template>
  <div class="login">
    <div class="left">
      <!-- 标题 -->
      <div class="header">
        <img class="logo" src="@/assets/login_icon.png" />
        <span class="title1">黑马页面</span>
        <span class="titleLine"></span>
        <span class="title2">用户登录</span>
      </div>

      <!-- 表单 -->
      <el-form
        class="form"
        :rules="rules"
        ref="form"
        :model="form"
        label-width="0px"
      >
        <el-form-item prop="phone">
          <el-input
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
            v-model="form.phone"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="form.password"
            show-password
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                placeholder="请输入验证码"
                v-model="form.code"
                prefix-icon="el-icon-key"
              ></el-input>
            </el-col>
            <el-col :span="8">
              <img
                class="code-img"
                @click="refreshCode"
                :src="codeUrl"
                alt=""
              />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass" :label="true">
            我已阅读同意
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="loginto" @click="loginClick"
            >登陆</el-button
          >
          <div class="line"></div>
          <el-button type="primary" class="registered" @click="registerClick"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 背景图片 -->
    <img src="@/assets/login_bg.png" alt="" />
    <register ref="register"></register>
  </div>
</template>

<script>
import '@/style/base.css'
import store from '@/store/index.js'
import register from './registered'
export default {
  components: {
    register
  },
  data () {
    return {
      codeUrl: process.env.VUE_APP_URL + '/captcha?type=login',
      form: {
        phone: '18511111111',
        password: '12345678',
        code: '',
        isPass: []
      },
      // 表单验证规则
      rules: {
        phone: [
          { required: true, message: '必填', trigger: 'change' },
          // 手机号验证
          {
            validator: (rule, value, callback) => {
              const _reg = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/
              if (_reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确的手机号'))
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 4, max: 12, message: '请输入4-12为的密码' }
        ],
        code: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 4, max: 4, message: '请输入正确的验证码' }
        ],
        isPass: [{ required: true, message: '请勾选协议', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 登录验证
    loginClick () {
      this.$refs.form.validate(async result => {
        if (result) {
          try {
            await store.dispatch('user/toLogining', this.form)
            console.log(store.state.user.token)
            this.$message.success('登录成功')
            const redirect = this.$route.query.redirect || '/layout/chart'
            this.$router.push(redirect)
          } catch {
            this.$message.error('登录失败')
          }
        } else {
          this.$message.error('验证失败')
        }
      })
    },
    refreshCode () {
      this.codeUrl =
        process.env.VUE_APP_URL + '/captcha?type=login&dfhdfh=' + Date.now()
    },
    // 打开注册弹框主键
    registerClick () {
      this.$refs.register.isShow = true
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  /* 弹性盒子布局 */
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;

  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 44px 42px;
    margin-top: 119px;
    margin-bottom: 119px;
    margin-left: 73px;
    .header {
      display: flex;
      //   上下居中
      align-items: center;
      .logo {
        width: 25px;
      }
      .title1 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin-left: 13px;
        margin-right: 13px;
      }

      .titleLine {
        width: 2px;
        height: 28px;
        background: #c7c7c7;
        margin-right: 13px;
      }
      .title2 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .form {
      margin-top: 31px;
      .code-img {
        width: 100%;
        height: 40px;
      }
      .loginto {
        width: 100%;
        margin-bottom: 26px;
      }
      .registered {
        width: 100%;
      }
    }
  }
}
</style>
